/* GLVertexArray.h
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#include <GL/gl.h>
#include <graphics/GLVertexData.h>

#ifndef GLVERTEXARRAY_H_
#define GLVERTEXARRAY_H_

template <typename Tdata>
class GLVertexArray
{
protected:
	GLVertexData<Tdata>* arrayData;

public:
	GLVertexArray(GLVertexData<Tdata>* data) : arrayData(data) {};
	virtual ~GLVertexArray() {};

	virtual void setupPointers()
	{
		glVertexPointer(arrayData->getComponentCount(), GL_FLOAT, 0, arrayData->getAddress());
		glEnableClientState(GL_VERTEX_ARRAY);
	}

	virtual void cleanupPointers()
	{
		glDisableClientState(GL_VERTEX_ARRAY);
	}

	virtual void draw(unsigned int renderType)
	{
		setupPointers();
		glDrawArrays(renderType, 0, arrayData->getVerticesCount());
		cleanupPointers();
	}

	virtual void drawIndexed(unsigned int renderType, unsigned int indexCount, unsigned int* indexes)
	{
		setupPointers();
		glDrawElements(renderType, indexCount, GL_UNSIGNED_INT, indexes);
		cleanupPointers();
	}
};

#endif /* GLVERTEXARRAY_H_ */
